package com.ruoyi.basebill.service.impl;

import java.util.HashSet;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basebill.domain.OmsStocktransferbillentry;
import com.ruoyi.basebill.mapper.OmsStocktransferbillMapper;
import com.ruoyi.basebill.domain.OmsStocktransferbill;
import com.ruoyi.basebill.service.IOmsStocktransferbillService;

/**
 * 库存调拨单Service业务层处理
 * 
 * @author xiefeng
 * @date 2023-02-15
 */
@Service
public class OmsStocktransferbillServiceImpl implements IOmsStocktransferbillService 
{
    @Autowired
    private OmsStocktransferbillMapper omsStocktransferbillMapper;

    @Autowired
    private OmsStocktransferbillServiceImpl stocktransferbillService;

    /**
     * 查询库存调拨单
     * 
     * @param id 库存调拨单主键
     * @return 库存调拨单
     */
    @Override
    public OmsStocktransferbill selectOmsStocktransferbillById(Long id)
    {
        return omsStocktransferbillMapper.selectOmsStocktransferbillById(id);
    }

    @Override
    public List<OmsStocktransferbill> selectOmsStocktransferbillByIds(HashSet<Long> ids)
    {
        return omsStocktransferbillMapper.selectOmsStocktransferbillByIds(ids);
    }

    /**
     * 查询库存调拨单列表
     * 
     * @param omsStocktransferbill 库存调拨单
     * @return 库存调拨单
     */
    @Override
    public List<OmsStocktransferbill> selectOmsStocktransferbillList(OmsStocktransferbill omsStocktransferbill)
    {
        return omsStocktransferbillMapper.selectOmsStocktransferbillList(omsStocktransferbill);
    }

    @Override
    public List<OmsStocktransferbill> selectOmsStocktransferbillDetailList(OmsStocktransferbill omsStocktransferbill) {
        return omsStocktransferbillMapper.selectOmsStocktransferbillDetailList(omsStocktransferbill);
    }

    /**
     * 新增库存调拨单
     * 
     * @param omsStocktransferbill 库存调拨单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOmsStocktransferbill(OmsStocktransferbill omsStocktransferbill)
    {
        int rows = omsStocktransferbillMapper.insertOmsStocktransferbill(omsStocktransferbill);
        stocktransferbillService.insertOmsStocktransferbillentry(omsStocktransferbill);
        return rows;
    }

    /**
     * 修改库存调拨单
     * 
     * @param omsStocktransferbill 库存调拨单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOmsStocktransferbill(OmsStocktransferbill omsStocktransferbill)
    {
        if (omsStocktransferbill.getOmsStocktransferbillentryList() != null && omsStocktransferbill.getOmsStocktransferbillentryList().size() != 0){
            omsStocktransferbillMapper.deleteOmsStocktransferbillentryByParentid(omsStocktransferbill.getId());
            stocktransferbillService.insertOmsStocktransferbillentry(omsStocktransferbill);
        }
        return omsStocktransferbillMapper.updateOmsStocktransferbill(omsStocktransferbill);
    }

    /**
     * 批量删除库存调拨单
     * 
     * @param ids 需要删除的库存调拨单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsStocktransferbillByIds(Long[] ids)
    {
        omsStocktransferbillMapper.deleteOmsStocktransferbillentryByParentids(ids);
        return omsStocktransferbillMapper.deleteOmsStocktransferbillByIds(ids);
    }

    /**
     * 删除库存调拨单信息
     * 
     * @param id 库存调拨单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsStocktransferbillById(Long id)
    {
        omsStocktransferbillMapper.deleteOmsStocktransferbillentryByParentid(id);
        return omsStocktransferbillMapper.deleteOmsStocktransferbillById(id);
    }

    /**
     * 新增库存调拨单分录信息
     * 
     * @param omsStocktransferbill 库存调拨单对象
     */
    public void insertOmsStocktransferbillentry(OmsStocktransferbill omsStocktransferbill)
    {
        List<OmsStocktransferbillentry> omsStocktransferbillentryList = omsStocktransferbill.getOmsStocktransferbillentryList();
        Long id = omsStocktransferbill.getId();
        if (StringUtils.isNotNull(omsStocktransferbillentryList))
        {
            List<OmsStocktransferbillentry> list = new ArrayList<OmsStocktransferbillentry>();
            for (OmsStocktransferbillentry omsStocktransferbillentry : omsStocktransferbillentryList)
            {
                omsStocktransferbillentry.setParentid(id);
                list.add(omsStocktransferbillentry);
            }
            if (list.size() > 0)
            {
                omsStocktransferbillMapper.batchOmsStocktransferbillentry(list);
            }
        }
    }

    @Override
    public List<OmsStocktransferbill> selectSumBillNumberIsNull() {
        return omsStocktransferbillMapper.selectSumBillNumberIsNull();
    }

    @Override
    public int updateSumBillNumberByIds(String sumBillNumber, ArrayList<Long> ids) {
        return omsStocktransferbillMapper.updateSumBillNumberByIds(sumBillNumber,ids);
    }
}
